﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using HM.UI.HMService;
using GalaSoft.MvvmLight.Command;
using HM.UI.ViewModels;
using System.Windows;

namespace HM.UI.Converters
{
    class PaymentsReportViewModel : INotifyPropertyChanged
    {
        private HMServiceClient proxy = new HMServiceClient();

        #region Ctors

        // Ctor for all payments
        public PaymentsReportViewModel()
        {
            AllPaymentsDataGrid = Visibility.Visible;
            OneTenantDataGrid = Visibility.Collapsed;
            this.RefreshPayments();
        }

        // Ctor for payments filtered by tenant id
        public PaymentsReportViewModel(Tenant t)
        {
            AllPaymentsDataGrid = Visibility.Collapsed;
            OneTenantDataGrid = Visibility.Visible;
            this.RefreshPaymentsByTennatId(t.Id);
        }

        // Ctor for payments filtered by date range
        public PaymentsReportViewModel(DateTime? from, DateTime? to)
        {
            AllPaymentsDataGrid = Visibility.Visible;
            OneTenantDataGrid = Visibility.Collapsed;
            this.RefreshPaymentsByDateRange(from, to);
        }
        #endregion

        #region Refresh

        private void RefreshPayments()
        {
            this.proxy.GetPaymentsCompleted += (s, e) =>
            {
                this.Payments = e.Result;
            };

            this.proxy.GetPaymentsAsync();
        }

        private void RefreshPaymentsByTennatId(int tennantId)
        {
            this.proxy.GetPaymentsByTenantsIdCompleted += (s, e) =>
            {
                this.Payments = e.Result;
            };

            this.proxy.GetPaymentsByTenantsIdAsync(tennantId);
        }

        private void RefreshPaymentsByDateRange(DateTime? from, DateTime? to)
        {
            this.proxy.GetPaymentsByDateRangeCompleted += (s, e) =>
            {
                this.Payments = e.Result;
            };

            this.proxy.GetPaymentsByDateRangeAsync(from, to.Value.AddDays(1).AddSeconds(-1));
            // This is the trick I used to fix a bug: the same date in 'from' and 'to' parameters didn't return any result
            // (although records from that same date were present in the DB),
            // because it compares both date and time, and time that comes from datepicker is midnight.
            // Here I send midnight of the 'from' parameter and the last second of the day in 'to' parameter.
        }

        #endregion

        #region Properties

        private IEnumerable<Payment> payments;
        public IEnumerable<Payment> Payments
        {
            get
            {
                return this.payments;
            }

            set
            {
                this.payments = value;
                OnPropertyChanged("Payments");
            }
        }


        #region DataGrids visibility properties
        private Visibility allPaymentsDataGrid;
        public Visibility AllPaymentsDataGrid
        {
            get
            {
                return allPaymentsDataGrid;
            }

            set
            {
                this.allPaymentsDataGrid = value;
                OnPropertyChanged("AllPaymentsDataGrid");
            }
        }

        private Visibility oneTenantDataGrid;
        public Visibility OneTenantDataGrid
        {
            get
            {
                return oneTenantDataGrid;
            }

            set
            {
                this.oneTenantDataGrid = value;
                OnPropertyChanged("OneTenantDataGrid");
            }
        }
        #endregion

        #endregion

        private void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}